#!/bin/sh

# Copyright (C) 2015, Wazuh Inc.
# OSSEC         Controls Wazuh
# Author:       Daniel B. Cid <dcid@ossec.net>
# Modified for slackware by Jack S. Lai

WAZUH_HOME=WAZUH_HOME_TMP
WAZUH_SERVER="${WAZUH_HOME}/usr/share/wazuh-server/bin/wazuh-server"
WAZUH_LOG="${WAZUH_HOME}var/log/wazuh-server/wazuh-server.log"

start() {
    start-stop-daemon --start --background --output ${WAZUH_LOG} --exec ${WAZUH_SERVER} -- start
}

stop() {
    SERVER_PID=$(find /var/run/wazuh-server/ -name wazuh-server-[0-9]*.pid | grep -o '[0-9]\+')
    if [ -n "$SERVER_PID" ]; then
        start-stop-daemon --stop --signal TERM --pid ${SERVER_PID}
    fi
}

status() {
    ${WAZUH_SERVER} status
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
status)
    status
    ;;
*)
    echo "*** Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0
